package com.syh.jn.scm.service.impl.operation.statistics;

import cn.redscarf.commons.wrapper.Restrictions;
import cn.redscarf.commons.wrapper.Wrapper;
import com.alibaba.dubbo.config.annotation.Service;
import com.syh.jn.scm.domain.operation.entity.project.OperationProject;
import com.syh.jn.scm.domain.operation.repository.project.OperationProjectRepository;
import com.syh.jn.scm.dto.operation.project.OperationProjectVo;
import com.syh.jn.scm.dto.operation.statistics.DirectProjectReportVo;
import com.syh.jn.scm.infrastructure.repository.utils.MybatisTemplate;
import com.syh.jn.scm.service.api.operation.statistics.DirectProjectReportService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class DirectProjectReportServiceImpl implements DirectProjectReportService {
  @Autowired
  private OperationProjectRepository operationProjectRepository;

  @Override
  public List<DirectProjectReportVo> directProjectReport(Map<String, Object> params) {
    List subProjectIds = new ArrayList<Long>();

    List<OperationProjectVo> projects = operationProjectRepository.findListPageView(
            Wrapper.build().append(Restrictions.eq("businessEntityId",params.get("businessEntityId"))),1,(Integer)params.get("pageSize"));

    projects.forEach(project -> {
      Wrapper we = Wrapper.build();
      we.append(Restrictions.rightLike("catalogCode",project.getCatalogCode()+"-"));
      List<OperationProject> subProjects =  operationProjectRepository.getList(we);
      subProjects.forEach(subProject ->{
        subProjectIds.add(subProject.getId());
      });
    });
    params.put("projectIds",subProjectIds);
    return MybatisTemplate.selectList("dirProReportQRepository.dirProReport", params);
  }


  @Override
  public Integer getDirectProjectCount(Map<String, Object> params) {
    List subProjectIds = new ArrayList<Long>();

    List<OperationProjectVo> projects = operationProjectRepository.findListPageView(
            Wrapper.build().append(Restrictions.eq("businessEntityId",params.get("businessEntityId"))),1,(Integer)params.get("pageSize"));

    projects.forEach(project -> {
      Wrapper we = Wrapper.build();
      we.append(Restrictions.rightLike("catalogCode",project.getCatalogCode()+"-"));
      List<OperationProject> subProjects =  operationProjectRepository.getList(we);
      subProjects.forEach(subProject ->{
        subProjectIds.add(subProject.getId());
      });
    });
    params.put("projectIds",subProjectIds);
    return MybatisTemplate.selectOne("dirProReportQRepository.dirProReportCount", params);
  }
}
